package com.example.afinal;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.afinal.Audio.FileListAdapter;
import com.example.afinal.Audio.FileUtil;
import com.example.afinal.Audio.PlayRecordingActivity;
import com.example.afinal.view.SpacesItemDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VoiceListActivity extends Fragment {
    View view;
    private FileListAdapter fileListAdapter;
    List<File> list = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_voice_list, container, false);
        initView();
        return view;

    }

    private void initView() {
        list = FileUtil.getWavFiles();
        RecyclerView rv_list_main = view.findViewById(R.id.rv_list_main);
        rv_list_main.addItemDecoration(new SpacesItemDecoration(0));//设置item间距
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//竖向列表
        rv_list_main.setLayoutManager(layoutManager);


        fileListAdapter = new FileListAdapter();
        fileListAdapter.setmNotes(list);
        rv_list_main.setAdapter(fileListAdapter);

        /*点击跳转到详情页面*/
        fileListAdapter.setOnItemClickListener(new FileListAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, File file) {
                //showToast(note.getTitle());
                Intent intent = new Intent(getContext(), PlayRecordingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("file", file);
                intent.putExtra("data", bundle);
                startActivity(intent);
            }
        });
        fileListAdapter.setOnItemLongClickListener(new FileListAdapter.OnRecyclerViewItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, final File file) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("提示");
                builder.setMessage("确定删除笔记？");
                builder.setCancelable(false);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /*int ret = noteDao.deleteNote(note.getId());
                        if (ret > 0) {
                            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                            //TODO 删除笔记成功后，记得删除图片（分为本地图片和网络图片）
                            //获取笔记中图片的列表 StringUtils.getTextFromHtml(note.getContent(), true);
                            refreshNoteList();
                        }*/
                        String filePath = file.getPath();
                        File file = new File(filePath);
                        if (file.isFile() && file.exists()) {
                            file.delete();
                            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                            refreshNoteList();
                        }
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();
            }
        });

    }

    //刷新笔记列表
    private void refreshNoteList() {
//        if ( list== null)
            list = FileUtil.getWavFiles();
        fileListAdapter.setmNotes(list);
        fileListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();

        refreshNoteList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

}
